<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Master_mahasiswa extends zulController{
	
	function __construct(){
		parent::__construct();
		$this->load->model("master_mahasiswa_model");
		$this->load->model("master_kelas_model");
		$this->load->model("aktifitas_mahasiswa_model");
		$this->checkLogin();
	}
	public function index()
	{
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
		$colModel['EDIT'] = array('Edit', 60, FALSE, 'center', 0);
		$colModel['PRESTASI'] = array('Cetak Detail Prestasi', 100, FALSE, 'center', 0);
		$colModel['SP'] = array('Lihat SP', 60, FALSE, 'center', 0);
		$colModel['CUTI'] = array('Lihat Cuti', 60, FALSE, 'center', 0);
		$colModel['NILAI'] = array('Lihat Nilai', 60, FALSE, 'center', 0);
		$colModel['NRP'] = array('NRP', 110, FALSE, 'center', 1);
		$colModel['NAMA_KELAS'] = array('Nama Kelas', 110, FALSE, 'center', 1);
		$colModel['NAMA'] = array('Nama Mahasiswa', 110, FALSE, 'center', 1);
        $colModel['ALAMAT'] = array('Alamat', 110, FALSE, 'center', 1);
        // $colModel['NO_TELP'] = array('No. Telp.', 110, FALSE, 'center', 1);
        // $colModel['TANGGAL_MASUK'] = array('Tanggal Mas', 110, FALSE, 'center', 1);
        $colModel['KELAS'] = array('Semester', 90, FALSE, 'center', 1);
        $colModel['TGLLULUS'] = array('Tanggal Lulus', 110, FALSE, 'center', 1);
        $colModel['STATUS'] = array('Status', 110, FALSE, 'center', 1);
        $colModel['TGLMASUK'] = array('Angkatan', 110, FALSE, 'center', 1);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Mahasiswa'
        );
        $buttons[] = array('Tambah', 'add', 'master_mahasiswa');
        // $buttons[] = array('Hapus', 'delete', 'master_mahasiswa');
        $buttons[] = array('separator');
        // $buttons[] = array('Pilih Semua', 'add', 'master_mahasiswa');
        // $buttons[] = array('Hapus Pilihan', 'delete', 'master_mahasiswa');
        // $buttons[] = array('separator');
		//$buttons[] = array('Rekap Stok Obat', 'print', 'master_mahasiswa');
		
        $grid_js = build_grid_js('flex1', site_url("master_mahasiswa/grid_mahasiswa"), $colModel, 'ID_MAHASISWA', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar Mahasiswa";
        $data['info'] = "Daftar Mahasiswa";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function master_mahasiswa(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				if (com=='Tambah')
				{
					location.href='" . base_url() . "index.php/master_mahasiswa/form_mahasiswa/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .base_url('index.php/master_mahasiswa/delete_kelas')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "master";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function grid_mahasiswa(){
		$valid_fields = array('ID_MAHASISWA', 'NRP', 'NAMA', 'ALAMAT', 'NO_TELP', 'SEMESTER', 'TANGGAL_LULUS', 'STATUS', 'NAMA_KELAS', 'TANGGAL_MASUK');
        $this->flexigrid->validate_post('ID_MAHASISWA', 'ASC', $valid_fields);
		$join	= array(
			array("table"=>"master_mahasiswa","join_key"=>"ID_KELAS","join_table"=>"master_kelas","join_type"=>"left")
		);
		$column	= array('ID_MAHASISWA', 'NRP', 'NAMA', 'ALAMAT', 'KELAS', 'TGLLULUS', 'master_mahasiswa.STATUS', 'NAMA_KELAS','TGLMASUK');
        $records = $this->master_mahasiswa_model->get_joinFG($join,FALSE,$column);
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_MAHASISWA,
                $no,
				"<a href='".site_url("/master_mahasiswa/form_mahasiswa/".$row->ID_MAHASISWA)."'><img src='". base_url()."assets/images/grid/edit.png'></a>",
				"<a href='".site_url("/master_mahasiswa/cetak_prestasi/".$row->ID_MAHASISWA)."'><img src='". base_url()."assets/images/print.png'></a>",
				"<a href='".site_url("/sp_mahasiswa/index/".$row->ID_MAHASISWA)."'><img src='". base_url()."assets/images/grid/magnifier.png'></a>",
				"<a href='".site_url("/cuti_mahasiswa/index/".$row->ID_MAHASISWA)."'><img src='". base_url()."assets/images/grid/magnifier.png'></a>",
				"<a href='".site_url("/nilai_mahasiswa/index/".$row->ID_MAHASISWA)."'><img src='". base_url()."assets/images/grid/magnifier.png'></a>",
				$row->NRP,
				$row->NAMA_KELAS,
				$row->NAMA,
				$row->ALAMAT,
				// $row->NO_TELP,
				$row->KELAS,
				($row->TGLLULUS == NULL || $row->TGLLULUS == "0000-00-00") ? ("-") : (date("d-m-Y",strtotime($row->TGLLULUS))),
				($row->STATUS == 0) ? ("Belum Lulus") : (($row->STATUS == 1) ? ("Lulus") : (($row->STATUS == 2) ? ("DO") : ("Cuti"))),
				($row->TGLMASUK == NULL || $row->TGLMASUK == "0000-00-00") ? ("-") : (date("Y",strtotime($row->TGLMASUK))),
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	function form_mahasiswa($parameter=FALSE){
		$this->load->library("form_validation");
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in">', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'nrp','label'=>'NRP','rules'=>'required'),
		array('field'=>'nama','label'=>'Nama Mahasiswa','rules'=>'required'),
		array('field'=>'alamat','label'=>'Alamat','rules'=>'required'),
		// array('field'=>'no_telp','label'=>'Nomor Telepon','rules'=>'required'),
		array('field'=>'semester','label'=>'Semester','rules'=>'required'),
		array('field'=>'status','label'=>'Status','rsules'=>'required'),
		array('field'=>'tgl_masuk','label'=>'Tanggal Masuk','rules'=>'required'),
		// array('field'=>'tgl_lulus','label'=>'Tanggal Lulus','rules'=>'required'),
		// array('field'=>'angkatan','label'=>'Angkatan','rules'=>'required'),
		array('field'=>'kelas','label'=>'Nama Kelas','rules'=>'required'),
		));
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] = $this->master_mahasiswa_model->get($parameter);
			}
			$data["parameter"]	= $parameter;
			$join	= array(
					array("table"=>"master_kelas","join_key"=>"ID_JURUSAN","join_table"=>"master_jurusan"),
					array("table"=>"master_kelas","join_key"=>"ID_PROGRAM","join_table"=>"master_program")
			);
			$column	= array("ID_KELAS","master_kelas.ID_JURUSAN","master_kelas.ID_PROGRAM","master_kelas.NAMA_KELAS","NAMA_JURUSAN","NAMA_PROGRAM");
			$data["qkelas"]= $this->master_kelas_model->get_join_by($join,array("STATUS"=>1),FALSE,$column);
			$data["active"] = "master";
			$data["content"]	= $this->load->view("form_mahasiswa",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$nrp	= $this->input->post("nrp");
			$nama	= $this->input->post("nama");
			$alamat	= $this->input->post("alamat");
			$no_telp	= $this->input->post("no_telp");
			$semester	= $this->input->post("semester");
			$status	= $this->input->post("status");
			$tgl_lulus	= $this->input->post("tgl_lulus");
			$tgl_masuk	= $this->input->post("tgl_masuk");
			// $angkatan	= $this->input->post("angkatan");
			$kelas	= $this->input->post("kelas");
			$data	= array(
			"ID_KELAS"=>$kelas,
			"NRP"=>$nrp,
			"NAMA"=>$nama,
			"ALAMAT"=>$alamat,
			// "NO_TELP"=>$no_telp,
			"TGLMASUK"=>date("Y-m-d",strtotime($tgl_masuk)),
			// "SEMESTER"=>$semester,
			"TGLLULUS"=>($tgl_lulus == "") ? (NULL) : date("Y-m-d",strtotime($tgl_lulus)),
			"STATUS"=>$status,
			// "ANGKATAN"=>$angkatan
			);
			$this->master_mahasiswa_model->save($data, $parameter);
			redirect("/master_mahasiswa");
		}
	}
	function delete_kelas(){
		$ids	= $this->input->post('items');
		$delete	= $this->master_mahasiswa_model->delete($ids);
		if($delete == TRUE){
			$msg ='Data yang anda pilih berhasil dihapus';
			$this->output->set_output($msg);
		} else {
			$msg ='Gagal menghapus data';
			$this->output->set_output($msg);
		}
	}
	function cetak_prestasi($id){
		$this->load->library('phpexcel');
		$this->load->library('PHPExcel/iofactory');
		$data["mahasiswa"]	= $this->master_mahasiswa_model->get($id);
		$data["query"]	= $this->aktifitas_mahasiswa_model->get_by(array("ID_MAHASISWA"=>$id,"JENIS"=>1));
		$this->load->view("cetak_prestasi",$data);
	}
}
?>